package com.ueffort.channel;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.unity3d.player.UnityPlayer;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ChannelSDK {

    private static Context mActivity;
    private static String channel= "";
    private static String id = "";
    
    static {

        mActivity = UnityPlayer.currentActivity;

//        Log.e("channel", "packageName: "+mActivity.getPackageName());
    }
    
    public static void GetChannel(final ChannelListener listener) {
        if (channel.isEmpty() || id.isEmpty()){
            try {
                ApplicationInfo appInfo = mActivity.getPackageManager().getApplicationInfo(mActivity.getPackageName(), PackageManager.GET_META_DATA);

                String c = appInfo.metaData.getString("channel");
                if (c == null || c.isEmpty()){
                    c = getChannel(mActivity);
                    id = "";
                }else{
                    String i = appInfo.metaData.getString("channelId");
                    if (c != null) channel = c;
                    if (i != null) id = i;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        listener.onResult(channel, id);
    }

    public static boolean NeedPrivacyDialog(Context context) {
        boolean need = false;
        try {
            mActivity = context;
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

            String c = appInfo.metaData.getString("privacyDialog");

            Log.e("channel", "needPrivacy: "+c);
            if (c == null || c.isEmpty()){
            }else if (c.equals("Yes")){
                need = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return need;
    }
    
    public static String getChannel(Context context) {
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        String ret = "";
        ZipFile zipfile = null;
        try {
            zipfile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipfile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = ((ZipEntry) entries.nextElement());
                String entryName = entry.getName();
                if (entryName.startsWith("META-INF/channel_")) {
                    ret = entryName;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String[] split = ret.split("_");
        String channel = "";
        if (split != null && split.length >= 2) {
            channel = ret.substring(split[0].length() + 1);
        }
        return channel;
    }
}
